package com.ruoyi.web.controller.tag;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.security.PermissionUtils;
import com.ruoyi.oos.self.domain.TagConfigSelf;
import com.ruoyi.oos.self.domain.TagLimitdtlSelf;
import com.ruoyi.oos.self.mapper.TagLimitdtlSelfMapper;
import com.ruoyi.oos.self.service.ITagConfigSelfService;
import com.ruoyi.oos.tag.domain.TagLimitdtl;
import com.ruoyi.oos.tag.service.ITagConfigService;
import com.ruoyi.oos.tag.service.impl.PiUtilBean;

import cn.hz.pi.api.util.DateUtil;

/**
 * 测点配置Controller
 * 
 * @author wzy1
 * @date 2020-06-30
 */
@Controller
@RequestMapping("/self/self")
public class TagConfigSelfController extends BaseController
{
    private String prefix = "self/self";

    @Autowired
    private ITagConfigSelfService tagConfigSelfService;
    @Autowired
    private ITagConfigService tagConfigService;
    
    @Autowired
    private TagLimitdtlSelfMapper selfMapper;

    @GetMapping()
    public String self()
    {
        return prefix + "/self";
    }

        /**
     * 查询测点配置列表
     */
    
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TagConfigSelf tagConfigSelf)
    {
        startPage();
       // ShiroUser user = (ShiroUser) SecurityUtils.getSubject().getPrincipal(); 
      //  String username = user.username;
      // String id =  PermissionUtils.getPrincipalProperty("id").toString();
        long userid = Long.valueOf(String.valueOf(PermissionUtils.getPrincipalProperty("userId")));
        tagConfigSelf.setUserid(userid);
        List<TagConfigSelf> list = tagConfigSelfService.selectTagConfigSelfList(tagConfigSelf);
        return getDataTable(list);
    }
    
    /**
     * 导出测点配置列表
     */
    @Log(title = "测点配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TagConfigSelf tagConfigSelf)
    {
        List<TagConfigSelf> list = tagConfigSelfService.selectTagConfigSelfList(tagConfigSelf);
        ExcelUtil<TagConfigSelf> util = new ExcelUtil<TagConfigSelf>(TagConfigSelf.class);
        return util.exportExcel(list, "self");
    }

    /**
     * 新增测点配置
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存测点配置
     */
    @Log(title = "测点配置", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TagConfigSelf tagConfigSelf)
    {
    	 long userid = Long.valueOf(String.valueOf(PermissionUtils.getPrincipalProperty("userId")));
    	 tagConfigSelf.setUserid(userid);
        return toAjax(tagConfigSelfService.insertTagConfigSelf(tagConfigSelf));
    }

    /**
     * 修改测点配置
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        TagConfigSelf tagConfigSelf = tagConfigSelfService.selectTagConfigSelfById(id);
        mmap.put("tagConfigSelf", tagConfigSelf);
        return prefix + "/edit";
    }

    /**
     * 修改保存测点配置
     */
    @Log(title = "测点配置", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TagConfigSelf tagConfigSelf)
    {
        return toAjax(tagConfigSelfService.updateTagConfigSelf(tagConfigSelf));
    }

    /**
     * 删除测点配置
     */
    @Log(title = "测点配置", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tagConfigSelfService.deleteTagConfigSelfByIds(ids));
    }
    @GetMapping("/echart/{id}")
    public String echart(@PathVariable("id") Long id, ModelMap mmap) throws Exception
    {
    	TagConfigSelf tagConfig = tagConfigSelfService.selectTagConfigSelfById(id);//查询测点
    	PiUtilBean brean = tagConfigService.getChartData(tagConfig.getPitagname(), DateUtil.addDay(DateUtil.getTime(), -1), DateUtil.getTime());////查询测点曲线数据
        List<Float> list =brean.getRetrunList();
        mmap.put("tagConfig", tagConfig.getTagname()+"("+DateUtil.addDay(DateUtil.getTime(), -1)+"至"+DateUtil.getTime()+")");
        mmap.put("list", list);
        mmap.put("xList",brean.getReturntimes());
        
        TagLimitdtlSelf limtitl = new TagLimitdtlSelf();
        limtitl.setConfigid(id);
        List<TagLimitdtlSelf> limittlList =  selfMapper.selectTagLimitdtlSelfList(limtitl);
        Float i = 2f;
       if(limittlList!= null && list.size()>0) {
    	   List<ChartBean> limitList = new ArrayList<ChartBean>();
    	   for (TagLimitdtlSelf tagLimitdtl : limittlList) {
    		   ChartBean bean = new ChartBean();
    		   Float limitvalue=  tagLimitdtl.getLimitval();
    		   if(limitvalue == null) {
    			   limitvalue =0f;
    		   }
    		   bean.setyAxis(limitvalue);
    		   limitList.add(bean);
		}
    	   mmap.put("limitList", limitList); 
       }
       
        return prefix + "/echartData";
    }
    
    
}
